<?php


namespace support\traits;


use support\constants\RespCode;
use support\Response;

trait ApiResponse
{
    public function success($data = [], $message = "操作成功", $code = RespCode::SUCCESS): Response
    {
        $data = [
            'message' => $message,
            'code' => $code,
            'data' => $data,
        ];
        if ($request_id = request()->request_id) {
            $data['request_id'] = $request_id;
        }
        return json($data);
    }


    /**
     * @param string $message
     * @param int $code
     * @param array $data
     * @return Response
     */
    public function error($message = "操作成功", $code = RespCode::ERROR, $data = []): Response
    {
        $data = [
            'message' => $message,
            'code' => $code,
            'data' => $data,
        ];
        if ($request_id = request()->request_id) {
            $data['request_id'] = $request_id;
        }
        return json($data);
    }
}